package com.shunli.cterminal.strategy;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.shunli.cterminal.enums.ConditionTypeEnum;
import com.shunli.cterminal.model.dto.TrafficPolicyStrategyDTO;
import com.shunli.cterminal.model.res.SystemTrafficPolicyVO;
import com.shunli.cterminal.model.res.TrafficDomainVO;
import com.shunli.cterminal.service.ISystemTrafficDomainConfigurationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 渠道策略
 * </p>
 *
 * @author daodao
 * @since 2025-04-25 15:21
 */
@Component
public class ChannelStrategy implements Strategy {
    @Resource
    private ISystemTrafficDomainConfigurationService systemTrafficDomainConfigurationService;

    /**
     * 获取条件类型  ConditionTypeEnum
     *
     * @return
     */
    @Override
    public String getConditionTypeEnum() {
        return ConditionTypeEnum.AND.getCode();
    }

    /**
     * 执行策略 结果
     *
     * @param beforeResult 上一个规则过滤后的结果
     * @return String
     */
    @Override
    public Set<SystemTrafficPolicyVO> parse(Set<SystemTrafficPolicyVO> beforeResult, TrafficPolicyStrategyDTO trafficPolicyStrategyDTO) {
        TrafficDomainVO trafficDomainVO = systemTrafficDomainConfigurationService.queryEffectiveByPromotionCode(trafficPolicyStrategyDTO.getPromotionCode());
        if (ObjectUtil.isNull(trafficDomainVO)) {
            return beforeResult;
        }
        beforeResult = beforeResult.stream()
                .filter(systemTrafficPolicyVO -> StrUtil.equals(trafficDomainVO.getProjectCode(), systemTrafficPolicyVO.getProjectCode()))
                .collect(Collectors.toSet());
        return beforeResult;
    }
}
